﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TarreOA.TarreOADAL;
using System.Web.Script.Serialization;
using System.Data;
using System.Data.SqlClient;

public partial class DutyReport_admin_DutyUserList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string param = Request.QueryString["term"].ToString();
        JavaScriptSerializer sc = new JavaScriptSerializer();
        string str = sc.Serialize(getDutyUserList(param));
        Response.Write(str);
        Response.End();
    }

    private List<string> getDutyUserList(string param)
    {
        List<string> result = null;
        SqlConnection conn = null;
        SqlDataReader reader = null;

        try
        {
            conn = (SqlConnection)DbHelper.Instance.CreateConnection();

            string sql = null;

            if (string.IsNullOrEmpty(param.Trim()))
            {
                sql = "select UserName from Usertb";
            }
            else
            {
                sql = "select UserName from Usertb where UserName like '%" + param + "%'";
            }
            
            SqlCommand cmd = new SqlCommand(sql,conn);
            conn.Open();
            reader = cmd.ExecuteReader();
            result = new List<string>();
            string str = null;
            while (reader.Read())
            {
                str =  reader["UserName"].ToString();
                result.Add(str);
            }
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if (reader != null)
            {
                reader.Dispose();
            }

            if (conn != null && conn.State == ConnectionState.Open)
            {
                conn.Close();
                conn.Dispose();
            }
        }

        return result;
    }
}